//
//  ABSCategoryListController.m
//  商品分类列表信息处理
//
//  Created by 我演示 on 2019/10/30.
//  Copyright © 2019 ALLSall. All rights reserved.
//

#import "ABSCategoryListController.h"
#import "ABSGoodsDetailViewController.h"


static NSString *const collectionViewCellId = @"CollectionViewCellId";


@interface ABSCategoryListController () <UICollectionViewDelegate,UICollectionViewDataSource , UICollectionViewDelegateFlowLayout>
/**dataSource*/
@property(nonatomic,strong) NSMutableArray *dataSource;

/**collectionView*/
@property(nonatomic,strong) UICollectionView *collectionView ;


@end

@implementation ABSCategoryListController

-(NSMutableArray *)dataSource {
    if (!_dataSource) {
        
        _dataSource = [NSMutableArray array];
        //1: 读取数据
        NSString *bundleFile= [[NSBundle mainBundle] pathForResource:@"CategoryList" ofType:@"json"];
        
        NSData *jsonData = [NSData dataWithContentsOfFile:bundleFile];
        
        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
        
        CategoryListFirstModel *first = [CategoryListFirstModel  mj_objectWithKeyValues:dict];
        
        CategoryDataModel *dataModel= first.data ;
        NSMutableArray *dataArray = [NSMutableArray array];
        
        for (int i = 0; i < dataModel.records.count;  i++ ) {
            CategoryDataRecordsModel *recordModel = [CategoryDataRecordsModel mj_objectWithKeyValues:dataModel.records[i]];
            [dataArray addObject:recordModel];
        }
        self.dataSource = dataArray ;
    }
    return _dataSource;
}


//2: 创建collectivVIew
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight ) collectionViewLayout:flowLayout];
        
        _collectionView.delegate = self ;
        _collectionView.dataSource = self ;
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.alwaysBounceVertical = YES ;
        _collectionView.showsVerticalScrollIndicator = NO ;
        _collectionView.showsHorizontalScrollIndicator = NO ;
        
        _collectionView.translatesAutoresizingMaskIntoConstraints = NO ;
        [_collectionView registerClass:[CategoryListCollectionCell class] forCellWithReuseIdentifier:collectionViewCellId];

    }
    
    return _collectionView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor ;
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.collectionView];
    
}

#pragma mark - 遵守对应的展示的数据源信息

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CategoryListCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCellId forIndexPath:indexPath];
    
    //数据源信息
    CategoryDataRecordsModel *model = self.dataSource[indexPath.row];
    cell.model  = model ;
//    cell.backgroundColor = [UIColor greenColor];
    
    return cell;
    
}


#pragma mark-设置行间距和列间距

// 列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section  {
    return 10;
}

// 行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  {
    return 10;
}

//内边距  UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>) 对于整体的布局
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return  UIEdgeInsetsMake(10, 10, 0, 10);
}

#pragma mark- 设置对应的尺寸和大小 并且和行间距有关系
-(CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return  CGSizeMake((ScreenWidth - 30)/2, (ScreenWidth - 30)/2 + 80 );
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    ABSGoodsDetailViewController *detail = [[ABSGoodsDetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    
}

@end
